#include "jobmanagementpage.h"
#include "ui_jobmanagementpage.h"

JobManagementPage::JobManagementPage(QWidget *parent) :
    MyWidget(parent),
    ui(new Ui::JobManagementPage)
{
    ui->setupUi(this);
    loadJobData();

    connect(ui->pushButtonInsert, &QPushButton::clicked, this, &JobManagementPage::on_pushButtonInsert_clicked);
    connect(ui->pushButtonModify, &QPushButton::clicked, this, &JobManagementPage::on_pushButtonModify_clicked);
    connect(ui->pushButtonDelete, &QPushButton::clicked, this, &JobManagementPage::on_pushButtonDelete_clicked);
}

JobManagementPage::~JobManagementPage()
{
    delete ui;
}

void JobManagementPage::loadJobData()
{
    QList<QVariantList> jobs = SqlHelper::getInstance().queryJobs();

    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "编号" << "员工姓名" << "职位" << "宿舍编号");

    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);

    for (const QVariantList &job : jobs) {
        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setItem(row, 0, new QTableWidgetItem(job[0].toString()));
        ui->tableWidget->setItem(row, 1, new QTableWidgetItem(job[1].toString()));
        ui->tableWidget->setItem(row, 2, new QTableWidgetItem(job[2].toString()));
        ui->tableWidget->setItem(row, 3, new QTableWidgetItem(job[3].toString()));
    }
}

void JobManagementPage::on_pushButtonInsert_clicked()
{
    bool ok;
    QString employeeName = QInputDialog::getText(this, "新增职位", "请输入员工姓名：", QLineEdit::Normal, "", &ok);
    if (!ok || employeeName.isEmpty()) return;

    QString position = QInputDialog::getText(this, "新增职位", "请输入职位：", QLineEdit::Normal, "", &ok);
    if (!ok || position.isEmpty()) return;

    QString dormitoryIdStr = QInputDialog::getText(this, "新增职位", "请输入宿舍编号：", QLineEdit::Normal, "", &ok);
    if (!ok || dormitoryIdStr.isEmpty()) return;

    bool isInt;
    int dormitoryId = dormitoryIdStr.toInt(&isInt);
    if (!isInt) {
        QMessageBox::warning(this, "输入错误", "宿舍编号必须为整数！");
        return;
    }

    bool success = SqlHelper::getInstance().insertJob(employeeName, position, dormitoryId);
    if (success) {
        QMessageBox::information(this, "新增成功", "职位新增成功！");
        loadJobData();
    } else {
        QMessageBox::critical(this, "新增失败", "职位新增失败！");
    }
}

void JobManagementPage::on_pushButtonModify_clicked()
{
    int row = ui->tableWidget->currentRow();
    if (row == -1) {
        QMessageBox::warning(this, "请选择一项", "请选择要修改的职位！");
        return;
    }

    int id = ui->tableWidget->item(row, 0)->text().toInt();
    QString currentEmployeeName = ui->tableWidget->item(row, 1)->text();
    QString currentPosition = ui->tableWidget->item(row, 2)->text();
    QString currentDormitoryId = ui->tableWidget->item(row, 3)->text();

    bool ok;
    QString employeeName = QInputDialog::getText(this, "修改职位", "请输入员工姓名：", QLineEdit::Normal, currentEmployeeName, &ok);
    if (!ok || employeeName.isEmpty()) return;

    QString position = QInputDialog::getText(this, "修改职位", "请输入职位：", QLineEdit::Normal, currentPosition, &ok);
    if (!ok || position.isEmpty()) return;

    QString dormitoryIdStr = QInputDialog::getText(this, "修改职位", "请输入宿舍编号：", QLineEdit::Normal, currentDormitoryId, &ok);
    if (!ok || dormitoryIdStr.isEmpty()) return;

    bool isInt;
    int dormitoryId = dormitoryIdStr.toInt(&isInt);
    if (!isInt) {
        QMessageBox::warning(this, "输入错误", "宿舍编号必须为整数！");
        return;
    }

    bool success = SqlHelper::getInstance().updateJob(id, employeeName, position, dormitoryId);
    if (success) {
        QMessageBox::information(this, "修改成功", "职位修改成功！");
        loadJobData();
    } else {
        QMessageBox::critical(this, "修改失败", "职位修改失败！");
    }
}

void JobManagementPage::on_pushButtonDelete_clicked()
{
    int row = ui->tableWidget->currentRow();
    if (row == -1) {
        QMessageBox::warning(this, "请选择一项", "请选择要删除的职位！");
        return;
    }

    int id = ui->tableWidget->item(row, 0)->text().toInt();
    bool success = SqlHelper::getInstance().deleteJob(id);
    if (success) {
        QMessageBox::information(this, "删除成功", "职位删除成功！");
        loadJobData();
    } else {
        QMessageBox::critical(this, "删除失败", "职位删除失败！");
    }
}
